﻿#region 命名空间

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.ControlPanel
{
    [Access("DeliveryMethod")]
    public partial class DeliveryMethod : WebFactory.PageBase
    {
        private readonly DeliveryMethodManage _myBLL = PolicyInjection.Create<DeliveryMethodManage>();
        private WinnerCMS.Model.DeliveryMethod model = new WinnerCMS.Model.DeliveryMethod();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var Action = Request["Action"];
                if (Action == "Add")
                {
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                    this.btnEdit.Visible = false;
                }
                else if (Action == "Edit")
                {
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                    this.btnEdit.Visible = true;
                    this.btnOK.Visible = false;


                    this.model = new DeliveryMethodManage().GetInfo(int.Parse(Request["ID"]));

                    this.txtMethodName.Text = this.model.MethodName;
                    this.txtIntro.Text = this.model.Intro;
                    this.cbkIsDefault.Checked = this.model.IsDefault;
                    this.cbkPass.Checked = this.model.Pass == 1 ? true : false;
                    this.txtCharge.Text = this.model.Charge.ToString("N2");
                }

                else
                {
                    BindList(1);
                }
            }
        }

        private void BindList(int PageNo)
        {
            this.GV_List.EmptyDataTemplate = GetTemplate(this);
            var pp = new PagingParam();
            pp.PageSize = this.PageNavigation1.PageSize;
            pp.PageNo = PageNo;

            this.GV_List.DataSource = this._myBLL.BindList(pp);
            this.GV_List.DataBind();
            this.PageNavigation1.RecordCount = pp.RecordCount;

            if (this.GV_List.Rows.Count <= 0)
            {
                this.DelSeletedID.Enabled = false;
            }
        }

        protected void DelSeletedID_Click(object sender, EventArgs e)
        {
            var res = this._myBLL.Delete(GetSelectedRow.GetIDs(this.GV_List));
            if (res.Errors.Length > 0)
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
                return;
            }
            BindList(1);
        }

        protected void PageNavigation1_PageChanging(object src, WebControls.PageChangingEventArgs e)
        {
            BindList(e.NewPageIndex);
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            this.model.Id = 0;
            this.model.MethodName = this.txtMethodName.Text.Trim();
            this.model.Intro = this.txtIntro.Text;
            this.model.Charge = decimal.Parse(this.txtCharge.Text.Trim());
            this.model.Pass = this.cbkPass.Checked ? 1 : 0;
            this.model.IsDefault = this.cbkIsDefault.Checked;


            var res = this._myBLL.Add(this.model);

            if (string.IsNullOrEmpty(res.Errors))
            {
                this.Panel1.Visible = false;
                this.Panel2.Visible = true;

                BindList(1);
            }
            else
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            this.model.Id = int.Parse(Request["ID"]);
            this.model.MethodName = this.txtMethodName.Text.Trim();
            this.model.Intro = this.txtIntro.Text;
            this.model.Charge = decimal.Parse(this.txtCharge.Text.Trim());
            this.model.Pass = this.cbkPass.Checked ? 1 : 0;
            this.model.IsDefault = this.cbkIsDefault.Checked;


            var res = this._myBLL.Add(this.model);
            if (string.IsNullOrEmpty(res.Errors))
            {
                this.Panel1.Visible = false;
                this.Panel2.Visible = true;

                BindList(1);
            }
            else
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("DeliveryMethod.aspx");
        }

        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Pass")
            {
                this._myBLL.PassOrNo((int) this.GV_List.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);

                BindList(this.PageNavigation1.PageNo);
            }
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var res = this._myBLL.Delete(this.GV_List.DataKeys[e.RowIndex].Value.ToString());
            if (res.Errors.Length > 0)
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
            }
            BindList(1);
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var tc = e.Row.Cells[4];

                if (tc.Text == "1")
                {
                    tc.Text = "默认";
                }
                else
                {
                    tc.Text = "";
                }

                tc = e.Row.Cells[5];
                var lb = tc.Controls[0] as LinkButton;
                if (lb.Text == "1")
                {
                    lb.Text = "<IMG alt=\"已审核\" src=\"../Images/success-true.gif\"> ";
                }
                else
                {
                    lb.Text = "<IMG alt=\"未审核\" src=\"../Images/failed-false.gif\"> ";
                }
            }
        }
    }
}